// PropertiesOfProduct.cpp: implementation of the PropertiesOfProduct class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PropertiesOfProduct.h"
#include "StringValue.h"
using namespace StringValue;
#include "PartFormat.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

namespace JointSystem
{

PropertiesOfProduct::PropertiesOfProduct( PartFormat* pf )
{
	m_btprops.InitHashTable( 5 );
	assert( pf );
	PropertiesOfProductFormat* ppf = pf->GetProperties();
	if ( ppf )
		Initialize( ppf );
}

PropertiesOfProduct::~PropertiesOfProduct()
{
	Clear();
}

void PropertiesOfProduct::Clear()
{
	POSITION pos = 0;
	pos = m_btprops.GetStartPosition();
	CString key;
	ProductProperty* rvalue = 0;
	while( pos )
	{
		m_btprops.GetNextAssoc( pos, key, (void*&)rvalue );
		if ( rvalue )
			delete rvalue;
	}
	m_btprops.RemoveAll();
}

void PropertiesOfProduct::Initialize( PropertiesOfProductFormat* popf )
{
	ProductFormatProperty* pfp = 0;
	POSITION pos = popf->Properties().GetStartPosition();
	while ( pos )
	{
		CString key;
		ProductFormatProperty* value = 0;
		popf->Properties().GetNextAssoc( pos, key, (void*&)value );
		assert( value );
		if ( value->Type() == eLong )
			Add( key, _variant_t( TextToLong( value->DefaultValue() ) ), value );
		else if ( value->Type() == eDouble )
			Add( key, _variant_t( TextToDouble( value->DefaultValue() ) ), value );
		else if ( value->Type() == eString )
			Add( key, _variant_t( value->DefaultValue() ), value );
	}
	Former( popf );
}

void PropertiesOfProduct::GenerateFieldItems( CString& fields )
{
	POSITION pos = 0;
	CString key, str;
	ProductProperty* pp = 0;

	fields.Empty();
	pos = m_btprops.GetStartPosition();
	while ( pos )
	{
		m_btprops.GetNextAssoc( pos, key, (void*&)pp );
		switch ( pp->Value().vt )
		{
		case VT_I4:
			LongToText( pp->Value(), str );
			fields += str;
			break;
		case VT_R8:
			DoubleToText( pp->Value(), str );
			fields += str;
			break;
		case VT_BSTR:
			str = (LPCTSTR)(_bstr_t)pp->Value();
			fields += _T( "\'" );
			fields += str;
			fields += _T( "\'" );
			break;
		default:
			AfxMessageBox( _T( "Unsupported property type" ) );
			return;
		}
		fields += _T( " AS " );
		fields += key;
		fields += _T( ", " );
	}
	fields.TrimRight();
	if ( fields.GetLength() > 0 )
		fields.Delete( fields.GetLength() - 1 );
}

bool PropertiesOfProduct::GetVariables( CVarArray& arvar ) const
{
	POSITION pos = 0;
	pos = m_btprops.GetStartPosition();
	CString key;
	EDVariable* pvar = 0;
	ProductProperty* rvalue = 0;
	while ( pos )
	{
		m_btprops.GetNextAssoc( pos, key, (void*&)rvalue );
		pvar = new EDVariable;
		pvar->SetName( key );
		switch ( rvalue->Value().vt )
		{
		case VT_I4:
			key.Format( _T( "%d" ), (long)rvalue->Value() );
			break;
		case VT_R8:
			key.Format( _T( "%f" ), (double)rvalue->Value() );
			break;
		case VT_BSTR:
			key.Format( _T( "\'%s\'" ), (LPCTSTR)(_bstr_t)rvalue->Value() );
			break;
		default:
			return false;
		}
		pvar->SetExpression( key );
		arvar.Add( pvar );
	}
	return true;
}

}